package com.codefish.framework.spring.aop.framework;

import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/14 下午 05:04
 */
public class ReflectiveMethodInvocation implements MethodInvocation {

    protected final Object target;
    protected final Method method;
    protected final Object[] args;

    public ReflectiveMethodInvocation(Object target, Method method, Object[] args) {
        this.target = target;
        this.method = method;
        this.args = args;
    }

    @Override
    public Method getMethod() {
        return method;
    }

    @Override
    public Object[] getArguments() {
        return args;
    }

    @Override
    public Object proceed() throws Throwable {
        try {
            return method.invoke(target, args);
        } catch (InvocationTargetException ex) {
            //当反射执行方法抛出异常时，抛出真正的异常
            throw ex.getTargetException();
        }
    }

    @Override
    public Object getThis() {
        return this.target;
    }

    @Override
    public AccessibleObject getStaticPart() {
        return null;
    }
}
